<?php
class Application_Model_DbTable_Scheduler extends Zend_Db_Table_Abstract
{

	protected $_name = 'scheduler';

	private function _makeUp($data)
	{
		$newData = array();
		$newData['campaign_id'] = $data['campaign_id'];
		$newData['list_ids'] = $data['list_ids'];
		$newData['sending_time'] = $data['sending_time'];
		$newData['created_user'] = $data['created_user'];
		$newData['created_time'] = $data['created_time'];
		$newData['smtp_id'] = $data['smtp_id'];

		return $newData;
	}

	public function getScheduler($id)
	{
		$id = (int)$id;
		$row = $this->fetchRow('id = ' . $id);
		if (!$row)
		{
			throw new Exception("Could not find row $id");
		}
		return $row->toArray();
	}

	public function addScheduler($data)
	{
		$data = $this->_makeUp($data);
		$this->insert($data);
	}

	public function updateScheduler( $id, $data)
	{
		$data = $this->_makeUp($data);
		$this->update($data, 'id = '. (int)$id);
	}

	public function deleteScheduler($id)
	{
		$this->delete('id =' . (int)$id);
	}
	
	public function getAllInfo()
	{
		$returnArray = $this->fetchAll()->toArray();
		
		$listContact = new Application_Model_DbTable_List();
		
		$Campaign = new Application_Model_DbTable_Campaign();
		
		$user = new Application_Model_DbTable_User();
		
		foreach ($returnArray as $key => $curentData)
		{
			$thisArray = array();
			foreach ($curentData as $index => $st)
			{
				$thisArray[$index] = $st;
			}
			
			$a =  explode(',', $curentData['list_ids']);
			
			$thisArray['list_ids'] =  array();

			$a = $listContact->getSomeList($a);
			
			foreach ($a as $index => $value)
			{
				$thisArray['list_ids'][$index] =  $value;
			}
			
			//$data['campaign_id'] = $Campaign->getCampaign($curentData['campaign_id']);
			
			$data['created_user'] = $user->getUsername($curentData['created_user']);
			
			$thisArray['sending_time'] = date('Y-m-d',$curentData['sending_time']);
			
			$thisArray['created_time'] = date('Y-m-d',$curentData['created_time']);
			
			$returnArray[$key] = $thisArray;
		}
		
		return $returnArray;
	}
	
	public function getAllListContact()
	{
		$list = new Application_Model_DbTable_List();
		return $list->getAllList();
	}
	
	public function getAllCampaign()
	{
		/* $campaigns =  Application_Model_DbTable_Campaign();
		return $campaigns->getAllList(); */
		
		return array('1'=>'Campaign1', '2'=>'Campaign2');
	}
	
	public function getUID()
	{
		return Application_Helper_Authenticate::getUid();
	}
	
	public function _getListContact($str)
	{
		$list = explode(',', $str);
		$listContact = new Application_Model_DbTable_List();
	}
}